/**
 * 日期工具
 */
function DateUtil(){
	now = new Date();                    //当前日期      
	nowDayOfWeek = now.getDay();         //今天本周的第几天      
	nowDay = now.getDate();              //当前日      
	nowMonth = now.getMonth();           //当前月      
	nowYear = now.getYear();             //当前年      
	nowYear += (nowYear < 2000) ? 1900 :0;  //
    today_zero = new Date(now.getFullYear(), now.getMonth(), now.getDate());//今天零点
    tomorr_zero = new Date(now.getFullYear(), now.getMonth(), now.getDate()+1);//明天零点
    yestay_zero = new Date(now.getFullYear(), now.getMonth(), now.getDate()-1);//昨天零点
    houday_zero = new Date(now.getFullYear(), now.getMonth(), now.getDate()+2);
    //todaytime = now.getTime() - today_zero.getTime();//当前时间～今天零点time
    //yestaytime = now.getTime() - yestay_zero.getTime();//当前时间～昨天零点time
}
module.exports = DateUtil;

/*var now = new Date();                    //当前日期      
var nowDayOfWeek = now.getDay();         //今天本周的第几天      
var nowDay = now.getDate();              //当前日      
var nowMonth = now.getMonth();           //当前月      
var nowYear = now.getYear();             //当前年      
nowYear += (nowYear < 2000) ? 1900 :0;  // 
 */
var now, nowDayOfWeek, nowDay, nowMonth, nowYear, today_zero, yestay_zero, tomorr_zero, houday_zero;

DateUtil.showNow = function showNow(){
    return now;
}

DateUtil.formatDateNew = function formatDateNew(date, format){
    var o = {
        "Y"  : date.getFullYear(),
        "M+" : date.getMonth()+1, //month
        "d+" : date.getDate(), //day
        "h+" : date.getHours(), //hour
        "m+" : date.getMinutes(), //minute
        "s+" : date.getSeconds(), //second
        "q+" : Math.floor((date.getMonth()+3)/3), //quarter
        "S" : date.getMilliseconds() //millisecond
    }
    if(/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));
    }
    for(var k in o) {
        if(new RegExp("("+ k +")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
        }
    }
    return format;
}

/**
 * 格式化日期：yyyy-MM-dd
 */      
DateUtil.formatDate = function formatDate(date) {
   var myyear = date.getFullYear();
   var mymonth = date.getMonth() + 1;
   var myweekday = date.getDate();
   if (mymonth < 10) {
       mymonth = "0" + mymonth;
    }
   if (myweekday < 10) {
       myweekday = "0" + myweekday;
    }
   return (myyear + "-" + mymonth + "-" + myweekday);
}
function formatDate(date) {
	return DateUtil.formatDate(date)
}
DateUtil.ForDateType = function ForDateType(strDatetime) {
    var strDate = strDatetime.substring(0,10)
      , myyear = strDate.substring(0, strDate.indexOf('-'))
      , monthStr = strDate.substring(strDate.indexOf('-') + 1,strDate.lastIndexOf('-'))
	  , firstNum = monthStr.substring(0, 1);
	if(firstNum === '0'){
		monthStr = monthStr.substring(1, 2);
	}
    var mymonth = parseInt(monthStr) - 1
      , myweekday = strDate.substring(strDate.lastIndexOf('-') + 1,strDate.length);
    return new Date(myyear, mymonth, myweekday);
}
function ForDateType(strDate) {
	return DateUtil.ForDateType(strDate);
}


/**
 * 格式化日期：yyyy-MM   
 */   
DateUtil.formatMonth = function formatMonth(date) {
   var myyear = date.getFullYear();
   var mymonth = date.getMonth() + 1;
   if (mymonth < 10) {
       mymonth = "0" + mymonth;
    }
   return (myyear + "-" + mymonth);
}
/**
 * 获得某月的天数      
 * @param myMonth
 * @returns {Number}
 */
function getMonthDays(myMonth) {
   var monthStartDate = new Date(nowYear, myMonth, 1);
   var monthEndDate = new Date(nowYear, myMonth + 1, 1);
   var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
   return days;
}
/**
 * 获得本季度的开始月份      
 * @returns {Number}
 */
DateUtil.getQuarterStartMonth = function getQuarterStartMonth() {
   var quarterStartMonth = 0;
   if (nowMonth < 3) {
       quarterStartMonth = 0;
    }
   if (2 < nowMonth && nowMonth < 6) {
       quarterStartMonth = 3;
    }
   if (5 < nowMonth && nowMonth < 9) {
       quarterStartMonth = 6;
    }
   if (nowMonth > 8) {
       quarterStartMonth = 9;
    }
   return quarterStartMonth;
}
/**
 * 获得本周几日期
 * @param days
 * @returns {Date}
 */
DateUtil.getCweekday = function getCweekday(days){
    var weekStartDate = new Date(nowYear, nowMonth, nowDay -nowDayOfWeek+days);
    return weekStartDate;
}
/**
 * 获得本周的开始日期
 * @returns
 */
DateUtil.getWeekStartDate = function getWeekStartDate() {
   var weekStartDate = new Date(nowYear, nowMonth, nowDay -nowDayOfWeek+1);
   return formatDate(weekStartDate);
}
/**
 * 获得本周的结束日期      
 * @returns
 */
DateUtil.getWeekEndDate = function getWeekEndDate() {
   var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 -nowDayOfWeek));
   return formatDate(weekEndDate);
}
/**
 * 获得上周几日期
 * @param days
 * @returns {*}
 */
DateUtil.getTweekday = function getTweekday(days){
    var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - days);
    return weekStartDate;
}
/**
 * 获得上周的开始日期      
 * @returns
 */
DateUtil.getWeekTStartDate = function getWeekTStartDate() {
   var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek -6);
   return formatDate(weekStartDate);
}
 
/**
 * 获得上周的结束日期   
 * @returns
 */   
DateUtil.getWeekTEndDate = function getWeekTEndDate() {
   var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 -nowDayOfWeek) - 8);
   return formatDate(weekEndDate);
}
/**
 * 获得下周几日期
 * @param days
 * @returns {*}
 */
DateUtil.getNweekday = function getNweekday(days){
    var weekStartDate = new Date(nowYear, nowMonth, nowDay -nowDayOfWeek + days);
    return weekStartDate;
}
/**
 * 获得下周开始日期
 * @returns {*}
 */
DateUtil.getWeekNStartDate = function getWeekNStartDate(){
    var weekStartDate = new Date(nowYear, nowMonth, nowDay -nowDayOfWeek+1 +7);
    return formatDate(weekStartDate);
}
/**
 * 获得下周结束日期
 * @returns {*}
 */
DateUtil.getWeekNEndDate = function getWeekNEndDate(){
    var weekEndDate = new Date(nowYear, nowMonth, nowDay + (7 -nowDayOfWeek) +7);
    return formatDate(weekEndDate);
}

/**
 * 获得本月的开始日期      
 * @returns
 */
DateUtil.getMonthStartDate = function getMonthStartDate() {
   var monthStartDate = new Date(nowYear, nowMonth, 1);
   return formatDate(monthStartDate);
}
/**
 * 获得本月的结束日期 
 * @returns
 */
DateUtil.getMonthEndDate = function getMonthEndDate() {
   var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
   return formatDate(monthEndDate);
}
 
/**
 * 获得上月的开始日期      
 * @returns
 */
DateUtil.getTMonthStartDate = function getTMonthStartDate() {
   var monthStartDate = new Date(nowYear, nowMonth - 1, 1);
   return formatDate(monthStartDate);
}
/**
 * 获得上月的结束日期      
 * @returns
 */
DateUtil.getTMonthEndDate = function getTMonthEndDate() {
   var monthEndDate = new Date(nowYear, nowMonth - 1, getMonthDays(nowMonth- 1));
   return formatDate(monthEndDate);
}
/**
 * 获得下个月开始日期
 * @returns {*}
 */
DateUtil.getNextMonthStartDate = function getNextMonthStartDate(){
    var monthStartDate = new Date(nowYear, nowMonth+1, 1);
    return formatDate(monthStartDate);
}
/**
 * 获得下个月结束日期
 * @returns {*}
 */
DateUtil.getNextMonthEndDate = function getNextMonthEndDate(){
    var monthEndDate = new Date(nowYear, nowMonth+1, getMonthDays(nowMonth+1));
    return formatDate(monthEndDate);
}

/**
 * 获得本季度的开始日期  
 * @returns
 */    
DateUtil.getQuarterStartDate = function getQuarterStartDate() {
   var quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
   return formatDate(quarterStartDate);
}
 
/**
 * 获得本季度的结束日期      
 * @returns
 */
DateUtil.getQuarterEndDate = function getQuarterEndDate() {
   var quarterEndMonth = getQuarterStartMonth() + 2;
   var quarterStartDate = new Date(nowYear, quarterEndMonth,getMonthDays(quarterEndMonth));
   return formatDate(quarterStartDate);
}
/**
 * 获得昨天日期  
 * @returns
 */
DateUtil.getTDay = function getTDay() {
   var dayStart = new Date(nowYear, nowMonth, nowDay - 1);
   return formatDate(dayStart);
}
/**
 * 获得今天日期  
 * @returns
 */
DateUtil.getNowDayStart = function getNowDayStart() {
   var dayStart = new Date(nowYear, nowMonth, nowDay);
   return formatDate(dayStart);
}
function getSetDateStart(dt, addDate) {
   addDate += 1;
   var date = ForDateType(dt);
   var myyear = date.getFullYear();
   var mymonth = date.getMonth();
   var myweekday = date.getDate() + addDate;
   if (myweekday < 0) {
       mymonth = date.getMonth() - 1;
       myweekday = getMonthDays(mymonth) + date.getDate() + addDate;
    }
   if (mymonth < 0) {
       myyear = date.getFullYear() - 1;
       mymonth = 11;
    }
   if (myweekday > getMonthDays(mymonth)) {
       myweekday = getMonthDays(mymonth) + date.getDate() + addDate;
       mymonth = mymonth + 1;
    }
   if (mymonth < 0) {
       myyear = date.getFullYear() - 1;
       mymonth = 11;
    }
   if (mymonth > 11) {
       myyear = date.getFullYear() + 1;
       mymonth = 1;
    }
   var dayStart = new Date(myyear, mymonth, myweekday);
   return formatDate(dayStart);
}
function getDateYears(date, format) {
   var temp;
   var myyear = date.getFullYear();
   var mymonth = date.getMonth() + 1;
   var myweekday = date.getDate();
 
   if (mymonth < 10) {
       mymonth = "0" + mymonth;
    }
   if (myweekday < 10) {
       myweekday = "0" + myweekday;
    }
   switch (format) {
       case "YYYY":
           temp = myyear;
           break;
       case "YY":
           temp = (myyear + "").substring(2, (myyear +"").length);
           break;
       case "MM":
           temp = mymonth;
           break;
       case "DD":
           temp = myweekday;
           break;
    }
   return temp;
}
/**
 * 日期比较
 */
DateUtil.compareDate = function compareDate(firstDate, secDate){
	var firstArr = firstDate.split('-')
	  , secArr = secDate.split('-')
	  , first_year = parseInt(firstArr[0])
	  , sec_year = parseInt(secArr[0])
	  , first_month = parseInt(firstArr[1])
	  , sec_month = parseInt(secArr[1])
	  , first_day = parseInt(firstArr[2])
	  , sec_day = parseInt(secArr[2]);
	if(first_year === sec_year){
		if(first_month === sec_month){
			if(first_day === sec_day){
				return '0';
			}else{
				return first_day > sec_day;
			}
		}else{
			return first_month > sec_month;
		}
	}else{
		return first_year > sec_year;
	}
}
/**
 * 获得便捷时间
 * @param date
 */
DateUtil.getZhTime = function getZhTime(strTime){
    if(strTime){
        var date= new Date(Date.parse(strTime.replace(/-/g, "/")))
          , datetime = date.getTime()
          , time = now.getTime() - date.getTime()
          , len = strTime.length > 16 ? 16 : strTime.length;
        if(time < 1800000 && time > 0){
            var mm = Math.round(time/60000);
            return '<span style="color: #3498DB">'+(mm > 0 ? mm+'分钟前' : '刚刚')+'</span>';
        }else if(datetime >= today_zero.getTime() && datetime < tomorr_zero.getTime()){
            return '<span style="color: #1ABC9C">今天'+strTime.substring(10,len)+'</span>'
        }else if(datetime >= yestay_zero.getTime() && datetime < today_zero.getTime()){
            return '<span style="color: #2C3E50">昨天'+strTime.substring(10,len)+'</span>'
        }else if(datetime >= tomorr_zero.getTime() && datetime < houday_zero.getTime()){
            return '<span style="color: #2C3E50">明天'+strTime.substring(10,len)+'</span>'
        }else if(datetime >= DateUtil.getTweekday(6).getTime() && datetime <= DateUtil.getTweekday(2).getTime()){
            return '<span>上周' + _getWeekday(date.getDay())+strTime.substring(10,len)+'</span>'
        }else if(datetime >= DateUtil.getNweekday(8).getTime() && datetime <= DateUtil.getNweekday(12).getTime()){
            return '<span>下周'+ _getWeekday(date.getDay())+strTime.substring(10,len)+'</span>'
        }else if(datetime >= DateUtil.getCweekday(1).getTime() && datetime <= DateUtil.getCweekday(5).getTime()){
            return '<span>本周'+ _getWeekday(date.getDay())+strTime.substring(10,len)+'</span>'
        }else{
            return strTime.substring(5,len);
        }
    }else{
        return strTime;
    }
}
DateUtil.getZhDate = function getZhDate(date, min){
    var datestr = date.getFullYear()+'年'
        +(date.getMonth()+1)+'月'
        +date.getDate()+'号 '
        if(!min){
            datestr += date.getHours()+'点'
                +date.getMinutes()+'分'
        }
        return datestr;
}
DateUtil.getWeekday = function getWeekday(date){
    return _getWeekday(date.getDay());
}
function _getWeekday(day){
    switch (day){
        case 1 : return '一';
            break;
        case 2 : return '二';
            break;
        case 3 : return '三';
            break;
        case 4 : return '四';
            break;
        case 5 : return '五';
            break;
        case 6 : return '六';
            break;
        case 0 : return '日';
            break;
    }
}

DateUtil.twoDateBetween = function twoDateBetween(first, second){
    return (first-second)/86400000;
}